package map;

import java.util.HashMap;
import java.util.Map;

public class HasnMapComputeTest {

    public static void main(String[] args) {

        Map<KeyClass,String> map = new HashMap<>();
        KeyClass k = new KeyClass(1,"1");
        map.computeIfAbsent(k, KeyClass::toString);
        k = new KeyClass(2,"2");
        map.computeIfAbsent(k, k::toString);
        System.out.println(map.get(new KeyClass(1,"1")));
        System.out.println(map.get(k));

        k = new KeyClass(3,"3");
        ClassA ca = new ClassA();
        map.computeIfAbsent(k, ca::toString);
        System.out.println(map.get(k));
        map.computeIfPresent(k, ca::changeKV);
        System.out.println(map.get(k));
        k = new KeyClass(4,"4");
        map.compute(k, ca::changeKV);
        System.out.println(map.get(k));

        Map<Integer,String> intMap = new HashMap<>();
        Integer t = 100;
//        intMap.computeIfAbsent(t, Integer::toString);
        System.out.println(t.toString());
        
        Map<String,String> strMap = new HashMap<>();
        String str = new String("100");
        strMap.computeIfAbsent(str, String::toString);
        System.out.println(str.toString());
    }

}
class ClassA {

    public String toString(KeyClass c) {
        return c.getS();
    }

    public String changeKV(KeyClass key, String value) {
        return key.getS()+":"+(value==null?"":value);
    }

}
